let requestUrl = null;

function addHandler() {
	let roleId = $("#role_id").combobox("getValue");

	if (roleId) {
		$("#roleId").combobox("setValue", roleId);
	}

	requestUrl = "/role_exp/insert";

	$("#exp_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#exp_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/role_exp/updateById";

		$("#id").val(rowData.id);
		$("#exp").numberspinner("setValue", rowData.exp);
		$("#roleId").combobox("setValue", rowData.roleId);
		$("#yfjy").numberspinner("setValue", rowData.yfjy);
		$("#total").numberspinner("setValue", rowData.total);

		$("#exp_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#exp_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function(bool) {
			if (bool) {
				ajaxPost("/role_exp/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#exp_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的记录", "warning");
	}
}

$(document).ready(function() {

	$("#role_id").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		editable: true,
		panelHeight: "auto",
		prompt: "-选择角色-",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#modeling_" + row.id).attr("src", data.image);
			}, error);

			return "<img height='18' id='modeling_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let roleId = $("#role_id").combobox("getValue");

		$("#exp_list").datagrid("reload", {
			roleId: roleId
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#exp").numberspinner({
		min: 0,
		width: 150,
		required: true
	});

	$("#yfjy").numberspinner({
		min: 0,
		width: 150,
		required: true
	});

	$("#total").numberspinner({
		min: 0,
		width: 150,
		required: true
	});

	$("#roleId").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		editable: true,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#role_modeling_" + row.id).attr("src", data.image);
			}, error);

			return "<img height='18' id='role_modeling_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	$("#exp_dialog").dialog({
		title: "角色经验",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#exp_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#exp_dialog").dialog("close");
						$("#exp_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#exp_form").form("clear");
				$("#exp_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_role_details * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;
	/**
	 * 可选页码
	 * @type {number[]}
	 */
	let pageList = [15, 30, 100, 200, 500];

	$("#exp_list").datagrid({
		url: "/role_exp/selectByPage",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		pageList: pageList,
		pageSize: pageList[0],
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}],
		columns: [[
			{field: "id", hidden: true},
			{field: "exp", title: "经验值", align: "center", width: 160},
			{field: "yfjy", title: "盈福经验", align: "center", width: 160},
			{field: "total", title: "总经验", align: "center", width: 160},
			roleDetailsColumn
		]]
	});

});